const axios = require('axios')
const { json } = require('body-parser')


let fpx = {
  "nuguri": {
    "bid": "pHNmN33BBgjQ",
    "tid": "pu1m3iNB1qqj",
    "id": "9023",
    "uid": "6516766535",
    "vid": "9023",
    "securityid": "5c2087cc0bd993acd18f52c688e39e33"
  },
  "tian": {
    "bid": "p3iBmumNBgjX",
    "tid": "pu1m3iNB1qqj",
    "id": "9118",
    "uid": "6516766535",
    "vid": "9118",
    "securityid": "e2796f43d84b4cd728088a1fed905d79"
  },
  "doinb": {
    "bid": "pNuizB6i6gjP",
    "tid": "pu1m3iNB1qqj",
    "id": "9044",
    "uid": "6516766535",
    "vid": "9044",
    "securityid": "196b4fe834cdbfd0a3d2222c9327d611"
  },
  "lqs": {
    "bid": "pu6HiH116gjU",
    "tid": "pu1m3iNB1qqj",
    "id": "9098",
    "uid": "6516766535",
    "vid": "9098",
    "securityid": "7bd1dcab941aba7dd34056977fc166da"
  },
  "lwx": {
    "bid": "pNm13NiHHgjW",
    "tid": "pu1m3iNB1qqj",
    "id": "9073",
    "uid": "6516766535",
    "vid": "9073",
    "securityid": "41a1323f59cba863e8a2ee57f33fa56c"
  }
}

function deal_header(tid, bid, sub = '') {
  let headers = {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
    "Connection": "keep-alive",
    "Content-Length": "109",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": sub,
    "DNT": "1",
    "Host": "sports.weibo.cn",
    "Origin": "https://sports.weibo.cn",
    "Referer":
      `https://sports.weibo.cn/rank/index?topicid=${tid}&bid=${bid}&portrait_only=1&disable_sinaurl=1&sinainternalbrowser=topnav&share_menu=1&disable_gesture_back=1`,
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent":
      "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Mobile Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
  }
  return headers
}

function deal_data(data) {
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  ret = ret.substring(0, ret.lastIndexOf('&'));
  return ret
}

function config(sub) {
  return new Promise(async (resolve, reject) => {
    try {
      let json = []
      for (let p in fpx) {
        console.log(p);
        let fpx_name = fpx[p]
        data = deal_data(fpx_name)
        let headers = deal_header(fpx[p]['tid'], fpx[p]['bid'], sub)
        let res = await axios({
          method: 'post',
          url: 'https://sports.weibo.cn/aj/rank/votenum?',
          headers: headers,
          data: data
        })
        let q = res.data
        // doinb  可投数  今日可送2个奖杯
        let { data: { pic, name, left_vote_num, mask_text, url } } = q
        msg = `${name}----${mask_text}`
        let info = {
          pic:pic,
          name:name,
          count:left_vote_num,
          mask_text:mask_text
        }
        json.push(msg)
        if (+(left_vote_num) !== 0) {
          sign(fpx[p]['securityid'], left_vote_num, url, headers)
        }
      }
      resolve(json)
    } catch (error) {
      reject(error)
    }
  })
}

function sign(securityid, count, url, headers) {
  let sign_url = 'https://sports.weibo.cn' + url
  send_data = {
    "is_follow": "1",
    "is_follow_rank": "1",
    "vote_num": count,
    "securityid": securityid
  }
  let data = deal_data(send_data)
  setTimeout(async () => {
    let res = await axios({
      method: 'post',
      url: sign_url,
      data: data,
      headers: headers
    })
    let { data: { msg } } = res
    console.log(msg);
  }, 2000);
}




module.exports = {
  config
}